<template>
	<view v-if="goods" class="container" :style="appThemeStyle">
		<!-- 顶部 -->
		<Navbar isZhanwei color="#000" fixed bg-color="#fff">
			<template #center>
				<view style="width: 500rpx;" @click="$navTo('pages/search/index')">
					<u-search disabled shape="round" :show-action="false"></u-search>
				</view>
			</template>
			<template #right>
				<u-icon @click="onShowShareSheet()" size="36" name="zhuanfa"></u-icon>
			</template>
		</Navbar>
		<!-- 顶部锚点坐标 -->
		<u-sticky offset-top="150">
			<div class="tabs-box">
				<div @click="scrollTo(item)" :class="activeKey==item.key?'active':'items'"
					v-for="(item,index) in keyList" :key="index">
					{{item.title}}
				</div>
			</div>
		</u-sticky>
		<!-- 商品图片轮播 -->
		<SlideImage  :images="goods.goods_images" />
		<!-- 商品信息 -->
		<view class="goods-infos m-top20">
			<div class="price">
				￥<text>{{ goods.goods_price_min}}</text>
			</div>
			<div class="good-names u-line-2">
				{{goods.goods_name}}
			</div>
			<div class="fl-box">
				<div class="inner" :style="{background:`url('${imgs.goodDetail}')`}">
					<div class="left">下单立返{{goods.fl}}元</div>
					<div class="right" @click="submit">领券购买</div>
				</div>
			</div>
		</view>
		<!-- 商品描述 -->
		<view  class="goods-content m-top20" id="bl">
			<view class="item-title b-f">
				<text>宝贝详情</text>
			</view>
			<view v-if="goods.content != ''" class="goods-content__detail b-f">
				<mp-html :content="goods.content" />
			</view>
		</view>

		<!-- 底部选项卡 -->
		<view class="footer-fixed">
			<view class="footer-container">
				<!-- 导航图标 -->
				<view class="foo-item-fast">
					<!-- 首页 -->
					<view class="fast-item fast-item--home" @click="onTargetHome">
						<view class="fast-icon">
							<text class="iconfont icon-shouye"></text>
						</view>
						<view class="fast-text">
							<text>首页</text>
						</view>
					</view>
					<view class="fast-item fast-item--home" @click="onShowShareSheet()">
						<view class="fast-icon">
							<u-icon name="zhuanfa"></u-icon>
						</view>
						<view class="fast-text">
							<text>分享</text>
						</view>
					</view>
				</view>
				<!-- 操作按钮 -->
				<view class="foo-item-btn">
					<view class="btn-wrapper">
						<view class="btn-item-deputy" @click="addFavatror">
							<text>{{is_collect?'已收藏':'加入收藏'}} </text>
						</view>
						<view class="btn-item-main" @click="submit">
							<text>立即购买</text>
							<view style="font-size: 20rpx;">下单约返{{goods.fl}}元</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 快捷导航 -->
		<!-- <shortcut bottom="120rpx" /> -->

		<!-- 分享菜单 -->
		<share-sheet goodType="other" v-model="showShareSheet" :shareTitle="goods.goods_name" :shareImageUrl="goods.goods_images[0]"
			:posterApiCall="posterApiCall" :posterApiParam="{ goodsId }" />
  
	</view>
</template>

<script>
	import WxofficialMixin from '@/core/mixins/wxofficial'
	import {
		getSceneData
	} from '@/core/app'
	import * as GoodsApi from '@/api/goods'
	import * as CartApi from '@/api/cart'
	import SettingModel from '@/common/model/Setting'
	import {
		GoodsTypeEnum
	} from '@/common/enum/goods'
	import Recommended from '@/components/recommended'
	import ShareSheet from '@/components/share-sheet'
	import CustomerBtn from '@/components/customer-btn'
	import SlideImage from './components/SlideImage'
	import Comment from './components/Comment'
	import Service from './components/Service'
	import Navbar from '../components/w-navbar/index.vue'
	import imgs from '@/utils/imgs'
	const postType = {
		'jd': 2,
		'pdd': 3
	}
	export default {
		components: {
			Navbar,
			Recommended,
			ShareSheet,
			CustomerBtn,
			SlideImage,
			Comment,
			Service
		},
		mixins: [WxofficialMixin],
		data() {
			return {
				is_collect: false,
				imgs,
				type: '',
				goods: '',
				goodsId: '',
				iconList: [{
						img: imgs.icons.icon1,
						text: '平台甄选'
					},
					{
						img: imgs.icons.icon2,
						text: '口碑好货'
					},
					{
						img: imgs.icons.icon3,
						text: '正品保障'
					},
					{
						img: imgs.icons.icon4,
						text: '极速发货'
					},
				],
				activeKey: 'a',
				keyList: [{
						key: 'a',
						title: '宝贝'
					},
					{
						key: 'b',
						title: '详情'
					},
				],
				showShareSheet:false
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		async onLoad(options) {
			let pages = getCurrentPages();
			this.pagesLength = pages.length;
			this.type = options.type
			this.goodsId = options.id
			this.getGoodsDetail()
		},

		methods: {
			scrollTo(item) {
				if (item.key == 'b') {
					const query = uni.createSelectorQuery();
					query.select('#bl').boundingClientRect()
					query.exec(res => {
						uni.pageScrollTo({
							scrollTop: res[0].top
						})
					})
				} else {
					uni.pageScrollTo({
						scrollTop: 0
					})
				}
				this.activeKey = item.key
			},
			onShowShareSheet() {
				this.showShareSheet = !this.showShareSheet
				// this.$toast('功能暂未开放，敬请期待！')
			}, 
			async addFavatror() {
				const postBody = {
					goods_id: this.goodsId,
					type: postType[this.type]
				}
				this.$api.my.collect(postBody).then((res) => {
					setTimeout(() => {
						this.$toast(res.message)
					}, 100)
					this.getGoodsDetail()
				})

			},
			// 获取商品信息
			getGoodsDetail() {
				switch (this.type) {
					case 'jd':
						this.getJDInfo()
						break;
					case 'pdd':
						this.getPDDInfo()
						break;
					default:
						break;
				}
			},
			onTargetHome() {
				this.$navTo('pages/index/index')
			},
			async getPDDInfo() {
				try{
					const res = await this.$api.pddApi.detail({
						id: this.goodsId
					})
					console.log(res);
					this.goods = {
						goods_images: [{
							preview_url: res.data.goods_image_url
						}],
						goods_price_min: res.data.min_group_price / 100,
						goods_name: res.data.goods_name,
						fl: res.data.promotion_rate,
						content: res.data.detail[0].goods_gallery_urls.map(i => `<img src="${i}"/>`).join(''),
						goods_sign: res.data.goods_sign
					}
					this.is_collect = res.data.is_collect 
				}catch(e){
					//TODO handle the exception
					console.log(e);
					console.log('err');
				}
			},
			async getJDInfo() {
				const res = await this.$api.jdApi.detail({
					id: this.goodsId
				})
				this.goods = {
					goods_images: res.data.imageInfo.imageList.map(i => ({
						preview_url: i.url
					})),
					goods_price_min: res.data.priceInfo.lowestPrice,
					goods_name: res.data.skuName,
					fl: res.data.set_commision_rate,
					content: res.data.imageInfo.imageList.map(i => `<img src="${i.url}"/>`).join(
						'')
				}
				this.is_collect = res.data.is_collect
			},
			submit() {
				switch (this.type) {
					case 'pdd':
						this.pddSubmit()
						break;
					default:
						this.$toast('功能暂未开放，敬请期待！')
						break;
				}
			},
			pddSubmit() {
				console.log(this.goods.goods_sign);
				this.$api.pddApi.getSortLink({
					goods_sign: this.goods.goods_sign
				}).then(res => {
					plus.runtime.openURL(res.data[0].mobile_url, function(res) {

					})
				}).catch(err => {
					console.log(err,'wt');
					// 	if (err.statusCode == 0) {
					// 		uni.showModal({
					// 			title: '拼多多返利需要先授权',
					// 			content: '跳转拼多多进行授权?',
					// 			confirmText: '去授权',
					// 			success: ({
					// 				cancel,
					// 				confirm
					// 			}) => {
					// 				if (confirm) {
					// 					if (plus.runtime.isApplicationExist({
					// 							pname: 'com.xunmeng.pinduoduo',
					// 							action: 'pinduoduo://'
					// 						})) {
					// 							this.$navTo('pageC/web/web',{
					// 								type:'pdd'
					// 							})
					// 						// plus.runtime.launchApplication({
					// 						// 		pname: 'com.xunmeng.pinduoduo'
					// 						// 	},
					// 						// 	function(e) {
					// 						// 		console.log(
					// 						// 			'Open system default browser failed: ' + e
					// 						// 			.message);
					// 						// 	}
					// 						// )
					// 					} else {
					// 						this.$toast("拼多多应用未安装")
					// 					}
					// 				}
					// 			}
					// 		})
					// 	}

				})
			}
		},


	}
</script>

<style>
	page {
		background: #fafafa;
	}
</style>
<style lang="scss" scoped>
	@import "./detail.scss";

	.fl-box {
		display: flex;
		align-items: center;

		.inner {
			width: 702rpx;
			height: 94rpx;
			display: flex;
			align-items: center;
			background-size: 100% 100%;
			background-repeat: no-repeat;
			padding: 0 26rpx;
			color: #fff;
			justify-content: space-between;

			.right {
				width: 142rpx;
				height: 58rpx;
				background: #FFFFFF;
				border-radius: 10rpx;
				font-weight: 500;
				font-size: 24rpx;
				color: #B70DFE;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}

	.tabs-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 750rpx;
		height: 88rpx;
		background: #FFFFFF;
		box-sizing: border-box;
		padding: 24rpx 134rpx;

		.itmes {}

		.active {
			position: relative;
		}

	}

	.active::after {
		content: '';
		position: absolute;
		bottom: -10rpx;
		width: 30rpx;
		height: 4rpx;
		background: #FF8C19;
		left: 50%;
		transform: translateX(-50%);
	}

	.goods-infos {
		padding: 24rpx;
		background-color: #fff;

		.price {
			margin-bottom: 28rpx;

			>text {
				font-weight: bold;
				font-size: 32rpx;
				color: #333130;
			}
		}

		.good-names {
			font-weight: 500;
			font-size: 32rpx;
			color: #333130;
			margin-bottom: 28rpx;
		}
	}
</style>